<?php


class signClassAction extends Action
{
    public function defaultAction()
    {
        return true;
    }

    public function infosAction()
    {
        $mid = $this->request('mid');
        $docInfo = m('document_circulation')->getone('id='.$mid, 'file');
        $flow = m('flow_bill')->getone('`table`=\'document_circulation\' and mid='.$mid, 'nowcheckid');

        $signPath = m('option')->getval('qmimgstr_'.$flow['nowcheckid']);
//        $personalPath = m('option')->getval('personal_'.$flow['nowcheckid']);
        $fileInfo = m('file')->getone('id='.$docInfo['file'], 'filepath');
        $sealPath = m('option')->getval('signature_'.$flow['nowcheckid']);
        if (empty(file_exists($signPath))) $this->msg(300,"请检测签名是否存在");
        if (empty(file_exists($sealPath))) $this->msg(300,"请检测公章是否存在");

        $data = array(
            'pdfPath' => './'.$fileInfo['filepath'],
            'signPath' => trim($signPath, './'),
            'sealPath' => trim($sealPath, './'),
        );
        $this->msg(200,'success', $data);
    }

    public function getInfoAction()
    {
        $postData = file_get_contents('php://input');
        $params = !empty($postData) ? json_decode($postData, true) : array();
        $signs = $params['signs'];
        $pdfHeight = $params['pdfHeight'];
        $pdfWidth = $params['pdfWidth'];
        $mid = $params['mid'];

        $docInfo = m('document_circulation')->getone('id='.$mid, 'file');
        $flow = m('flow_bill')->getone('`table`=\'document_circulation\' and mid='.$mid, 'nowcheckid');
        $signPath = m('option')->getval('qmimgstr_'.$flow['nowcheckid']);
//        $personalPath = m('option')->getval('personal_'.$flow['nowcheckid']);
        $fileInfo = m('file')->getone('id='.$docInfo['file'], 'filepath');
        $sealPath = m('option')->getval('signature_'.$flow['nowcheckid']);
        if (empty(file_exists($signPath))) $this->msg(300,"请检测签名是否存在");
        if (empty(file_exists($sealPath))) $this->msg(300,"请检测公章是否存在");
        $poi = array();
//        array(
//            array('page' => 1, 'sign' => true, 'signx' => 100 , 'signy' => 50),
//            array('page' => 1, 'personal'=>false,'personalx'=>100,'personaly'=>50),
//            array('page' => 1, 'seal' => true, 'sealx' => 100 , 'sealy' => 50),
//        );
        $signs = json_decode($signs);
        foreach ($signs as $sign) {
            foreach ($sign as $k => $v) {
                if($v->width == $v->height) {
                    $poi[] = array('page' => $v->pageNum, 'seal' => true, 'sealx' => $v->left , 'sealy' => $v->top);
                }else{
                    $poi[] = array('page' => $v->pageNum, 'sign' => true, 'signx' => $v->left , 'signy' => $v->top);
                }
            }
        }

        if ($pdfHeight > $pdfWidth) {
            $orientation = 'P';
            $widthHeight = array($pdfHeight, $pdfWidth);
        } else {
            $orientation = 'L';
            $widthHeight = array($pdfHeight, $pdfWidth);
        }

        $path = c('phpOffice')->setSign($fileInfo['filepath'], $poi, array(
            'sign' => $signPath,
            'personal' => '',
            'seal' => $sealPath
        ), $orientation, 'px', $widthHeight);

        $this->msg(200, 'success', array('path' => $path));
    }
}